package cn.bytecollege.controller;

import cn.bytecollege.constant.Resource;
import cn.bytecollege.entity.RoomCategory;
import cn.bytecollege.service.RoomCategoryService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author Mr.H
 */
@WebServlet(name="RoomCategoryAddController",value = "/roomCategoryAdd")
public class RoomCategoryAddController extends HttpServlet {
    private RoomCategoryService roomCategoryService=null;
    public RoomCategoryAddController(){
        roomCategoryService=new RoomCategoryService();
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       request.getRequestDispatcher(Resource.PAGE_LOCATION+"/roomCategoryAdd.jsp").forward(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String msg=null;
        //接收参数
        String name=request.getParameter("name");

        //将参数封装到RoomCategory对象中
        RoomCategory roomCategory=roomCategoryService.findRoomCategoryByCategoryName(name);
        if (roomCategory!=null){
            msg="该类型已存在请重新输入！";
            request.setAttribute("msg",msg);
            request.getRequestDispatcher(Resource.PAGE_LOCATION+"/roomCategoryAdd.jsp").forward(request,response);
            return;
        }
        //代码执行到这说明该分类未存在，封装数据然后调用Service方法
        RoomCategory rc=new RoomCategory();
        rc.setRoomCategoryName(name);
        rc.setDeleteFlag(0);
        roomCategoryService.save(rc);
        //跳转页面
        response.sendRedirect(request.getContextPath()+"/roomCategoryList");
    }
}
